﻿@model OrderModel.AddOrderProductModel

@{
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    //page title
    ViewBag.Title = string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId);
    //active menu item (system name)
    Html.SetActiveMenuItemSystemName("Orders");
}

@Html.AntiForgeryToken()
<div class="content-header clearfix">
    <h1 class="pull-left">
        @string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId)
        <small><i class="fa fa-arrow-circle-left"></i>@Html.ActionLink(T("Admin.Orders.Products.AddNew.BackToOrder").Text, "Edit", new { id = Model.OrderId })</small>
    </h1>
    <div class="pull-right">
        &nbsp;
    </div>
</div>

<div class="content">
    <div class="form-horizontal">
        <div class="panel-group">
            <div class="panel panel-default panel-search">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.NopLabelFor(model => model.SearchProductName)
                                </div>
                                <div class="col-md-8">
                                    @Html.NopEditorFor(model => model.SearchProductName)
                                </div>
                            </div>
                            <div class="form-group" @(Model.AvailableCategories.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                <div class="col-md-4">
                                    @Html.NopLabelFor(model => model.SearchCategoryId)
                                </div>
                                <div class="col-md-8">
                                    @Html.NopDropDownList("SearchCategoryId", Model.AvailableCategories)
                                </div>
                            </div>
                            <div class="form-group" @(Model.AvailableManufacturers.SelectionIsNotPossible() ? Html.Raw("style=\"display:none\"") : null)>
                                <div class="col-md-4">
                                    @Html.NopLabelFor(model => model.SearchManufacturerId)
                                </div>
                                <div class="col-md-8">
                                    @Html.NopDropDownList("SearchManufacturerId", Model.AvailableManufacturers)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-4">
                                    @Html.NopLabelFor(model => model.SearchProductTypeId)
                                </div>
                                <div class="col-md-8">
                                    @Html.NopDropDownList("SearchProductTypeId", Model.AvailableProductTypes)
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-8 col-md-offset-4">
                                    <button type="button" id="search-products" class="btn btn-primary btn-search">
                                        <i class="fa fa-search"></i>
                                        @T("Admin.Common.Search")
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="panel panel-default">
                <div class="panel-body">
                    <div id="products-grid"></div>

                    <script>
                        $(document).ready(function() {
                            $("#products-grid").kendoGrid({
                                dataSource: {
                                    type: "json",
                                    transport: {
                                        read: {
                                            url: "@Html.Raw(Url.Action("AddProductToOrder", "Order"))",
                                            type: "POST",
                                            dataType: "json",
                                            data: additionalData
                                        }
                                    },
                                    schema: {
                                        data: "Data",
                                        total: "Total",
                                        errors: "Errors"
                                    },
                                    error: function(e) {
                                        display_kendoui_grid_error(e);
                                        // Cancel the changes
                                        this.cancelChanges();
                                    },
                                    pageSize: @(defaultGridPageSize),
                                    serverPaging: true,
                                    serverFiltering: true,
                                    serverSorting: true
                                },
                                pageable: {
                                    refresh: true,
                                    pageSizes: [@(gridPageSizes)],
                                    @Html.Partial("_GridPagerMessages")
                                },
                                editable: {
                                    confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                    mode: "inline"
                                },
                                scrollable: false,
                                //selectable
                                selectable: true,
                                columns: [
                                {
                                    field: "Name",
                                    title: "@T("Admin.Common.Select")",
                                    width: 50,
                                    template: '<button type="button" onclick="addProductToOrder(#=Id#)" class="btn btn-default">@T("Admin.Common.Select")</button>'
                                },{
                                    field: "Id",
                                    title: "ID",
                                    width: 50
                                }, {
                                    field: "Name",
                                    title: "@T("Admin.Orders.Products.AddNew.Name")",
                                    width: 400
                                }, {
                                    field: "Sku",
                                    title: "@T("Admin.Orders.Products.AddNew.SKU")",
                                    width: 100
                                }
                                ]
                            });
                        });
                    </script>
                    <script type="text/javascript">
                        $(document).ready(function() {
                            $('#search-products').click(function() {
                                var grid = $('#products-grid').data('kendoGrid');
                                grid.dataSource.page(1); //new search. Set page size to 1
                                //grid.dataSource.read(); we already loaded the grid above using "page" function
                                return false;
                            });
                        });

                        $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function(event) {
                            if (event.keyCode == 13) {
                                $("#search-products").click();
                                return false;
                            }
                        });

                        function additionalData() {
                            var data = {
                                SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
                                SearchCategoryId: $('#SearchCategoryId').val(),
                                SearchManufacturerId: $('#SearchManufacturerId').val(),
                                SearchProductTypeId: $('#SearchProductTypeId').val()
                            };
                            addAntiForgeryToken(data);
                            return data;
                        }

                        function addProductToOrder(productId) {
                            //load "product details page" block
                            var productDetailsActionUrl = '@Html.Raw(Url.Action("AddProductToOrderDetails", "Order", new {orderId = Model.OrderId, productId = "productidplaceholder"}))';
                            productDetailsActionUrl = productDetailsActionUrl.replace("productidplaceholder", productId);
                            setLocation(productDetailsActionUrl);
                        }
                    </script>
                </div>
            </div>
            <div id="productdetails">
            </div>
            @Html.HiddenFor(model => model.OrderId)
        </div>
    </div>
</div>
